<template>
  <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="isShowDialog" @close="btnCancel">
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="120px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="btnCancel">取消</el-button>
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addRole, getRoleList, upSysRole } from '@/api/setting'
export default {
  props: {
    // 将显示与隐藏传给父元素
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 专门接收新增或者编辑的编辑的表单数据
      roleForm: {
        description: '',
        name: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async getRoleList(id) {
      this.roleForm = await getRoleList(id)
      console.log(this.roleForm)
    },
    btnCancel() {
      // 清空表单数据
      this.roleForm = {
        name: '',
        description: ''
      }
      // 清空表单校验
      this.$refs.roleForm.resetFields()
      //   this.$refs.addForm.resetFields()
      // 确保弹窗已经关闭
      this.$emit('update:isShowDialog', false)
    },
    // 确认按钮
    async btnOK() {
      // 校验表单
      await this.$refs.roleForm.validate()
      // 发送请求
      if (this.roleForm.id) {
        // 编辑
        await upSysRole({ ...this.roleForm }) // 新增
      } else {
        await addRole({ ...this.roleForm }) // 新增
      }
      // 提示用户
      this.$message.success('操作成功')
      // 关闭弹窗
      //   this.isShowDialog = false
      this.$emit('update:isShowDialog', false)
      // 更新页面
      this.$emit('addRole')
    }
  }
}
</script>

<style>

</style>
